package org.advanced.socket;

import java.io.*;
import java.net.Socket;

/**
 * @author John.zhang
 * @version Id: ClientSockets, v 1.0 2020/12/14 15:50 ZhiYong Exp $
 */
public class ClientSockets {

    public static void main(String[] args) {
        final int DEFAULT_PORT = 8082;
        try {
            //建立一个连接
            Socket socket = new Socket("localhost", DEFAULT_PORT);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedWriter.write("我是客户端发送的消息:client-01\n");
            bufferedWriter.flush();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = bufferedReader.readLine();
            System.out.println("收到服务端的消息："+line);

            bufferedReader.close();
            bufferedWriter.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
